package protocol;

import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicBoolean;

import protocol.meta.PageParam;
import protocol.meta.User;
import protocol.transactions.CancelFollowTransaction;
import protocol.transactions.CellPhoneRegTransaction;
import protocol.transactions.CheckTransaction;
import protocol.transactions.CheckVersionTransaction;
import protocol.transactions.ExternalUserRegTransaction;
import protocol.transactions.FeedBackTransaction;
import protocol.transactions.FollowTransaction;
import protocol.transactions.GetAdaviceListTransaction;
import protocol.transactions.GetAppSettingTransaction;
import protocol.transactions.GetCaptchaTrasaction;
import protocol.transactions.GetDynamicIdsTransaction;
import protocol.transactions.GetDynamicListTransaction;
import protocol.transactions.GetFansListTransaction;
import protocol.transactions.GetFollowListTransaction;
import protocol.transactions.GetGuessDiscussListTransaction;
import protocol.transactions.GetGuessFriendListTransaction;
import protocol.transactions.GetHotestSubjectListTransaction;
import protocol.transactions.GetIdKeyTransaction;
import protocol.transactions.GetLatestSourceListTransaction;
import protocol.transactions.GetPopDiscussListTransaction;
import protocol.transactions.GetPopFriendListTransaction;
import protocol.transactions.GetRecommAppTransaction;
import protocol.transactions.GetRecommendSubjectListTransaction;
import protocol.transactions.GetSourceDetailTransaction;
import protocol.transactions.GetSubjectDetailTransaction;
import protocol.transactions.GetSubjectListTransaction;
import protocol.transactions.GetSubscribeListTransaction;
import protocol.transactions.GetTopicListTransaction;
import protocol.transactions.GetUserDetailTransaction;
import protocol.transactions.LoginTransaction;
import protocol.transactions.LogoutTransaction;
import protocol.transactions.ModifyCellPhonePwTransaction;
import protocol.transactions.PostLogTransaction;
import protocol.transactions.SearchSubjectAndSourceTransaction;
import protocol.transactions.SetAppSettingTransaction;
import protocol.transactions.SubscribeTransaction;
import protocol.transactions.SyncBindingInfoTrasaction;
import protocol.transactions.TransactionCommentInfo;
import protocol.transactions.TransactionFeedback;
import protocol.transactions.TransactionForwardInfo;
import protocol.transactions.TransactionGetAtDiscussList;
import protocol.transactions.TransactionGetCommentDiscussList;
import protocol.transactions.TransactionGetInfoCfp;
import protocol.transactions.TransactionGetInfoDetail;
import protocol.transactions.TransactionGetInfoIds;
import protocol.transactions.TransactionGetInfoList;
import protocol.transactions.TransactionGetPraiseDiscussList;
import protocol.transactions.TransactionGetSourceInfoList;
import protocol.transactions.TransactionGetSubjectInfoList;
import protocol.transactions.TransactionGetTopicInfoList;
import protocol.transactions.TransactionGetUserDiscussList;
import protocol.transactions.TransactionHeadRecommend;
import protocol.transactions.TransactionLoopBack;
import protocol.transactions.TransactionPostDiscuss;
import protocol.transactions.TransactionPraiseInfo;
import protocol.transactions.TransactionReport;
import protocol.transactions.TransactionSourceInputTipList;
import protocol.transactions.TransactionSubjectInputTipList;
import protocol.transactions.TransactionTopicInputTipList;
import protocol.transactions.TransactionUnReadInfoCount;
import protocol.transactions.TransactionUpload;
import protocol.transactions.TransactionUpload.UploadPrama;
import protocol.transactions.TransactionUserInputTipList;
import protocol.transactions.UnsubscribeTransaction;
import protocol.transactions.UpdateUserTransaction;
import protocol.transactions.UrsUserRegTransaction;
import protocol.transactions.XoneBaseTransaction;
import android.content.Context;

import com.netease.http.HttpDataChannel;
import com.netease.http.HttpEngine;
import com.netease.pkgRelated.XoneConsts;
import com.netease.pkgRelated.XoneConsts.ACCOUNT_TYPE;
import com.netease.service.BaseService;
import com.netease.task.Transaction;
import com.netease.task.TransactionEngine;

public class XoneService extends BaseService {
    public static final String TAG = "XoneService";

    private static XoneService mInstance = null;
    private GroupTransactionListener mGroupListener;

    /**用于后台重登录时需要等待登录成功才能执行的请求缓存*/
    private AtomicBoolean mIsLoging = new AtomicBoolean(false);
    private LinkedList<Transaction> mWattingLoginList;


    public static XoneService getInstance() {
        if (mInstance == null) {
            mInstance = new XoneService();
        }
        return mInstance;
    }

    private XoneService() {
        super(new HttpDataChannel(TransactionEngine.Instance(), new HttpEngine(
                3, Thread.NORM_PRIORITY - 1)));
        mGroupListener = new GroupTransactionListener();
        mWattingLoginList = new LinkedList<Transaction>();
    }

    public void addListener(XoneCallBack listener){
        mGroupListener.addListener(listener);
    }

    public void removeListener(XoneCallBack listener){
        mGroupListener.removeListener(listener);
    }
    public void addToWattingList(Transaction trans){
        synchronized (mWattingLoginList) {
            mWattingLoginList.add(trans);
        }
    }
    public LinkedList<Transaction> getWattingList(){
        synchronized (mWattingLoginList) {
            LinkedList<Transaction> list = new LinkedList<Transaction>(mWattingLoginList);
            mWattingLoginList.clear();
            return list;
        }
    }
    @Override
    public int beginTransaction(Transaction trans) {
        trans.setListener(mGroupListener);
        return super.beginTransaction(trans);
    }
    public void setIsLoging(boolean isloging){
        mIsLoging.set(isloging);
    }
    public boolean isLoging() {
        return mIsLoging.get();
    }
    /**
     * 获取ID和密钥
     * @return
     */
    public int doGetIdKey(){
        GetIdKeyTransaction t = new GetIdKeyTransaction();
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 登录
     * @param type
     * @param username
     * @param pwd
     * @return
     */

    public int doLogin(int type,String username,String pwd){
        LoginTransaction t = new LoginTransaction(type,username, pwd);
        beginTransaction(t);
        if(!isLoging()){
            setIsLoging(true);
        }
        return t.getId();
    }
    /**
     * 检查手机号码唯一性
     * @param type
     * @param username
     * @param pwd
     * @return
     */
    public int doCheckPhoneNumber(String phonenumber){
        CheckTransaction t = new CheckTransaction(phonenumber);
        t.setNeedLogin(true);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 获取资讯头条推荐
     * @return
     */
    public int doHeadRecommend() {
        XoneBaseTransaction t = new TransactionHeadRecommend();
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 获取输入提示关注用户列表
     * @return
     */
    public int doUserInputTipList(String type) {
        XoneBaseTransaction t = new TransactionUserInputTipList(type);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 获取输入提示订阅资讯源列表
     * @return
     */
    public int doSourceInputTipList(String type) {
        XoneBaseTransaction t = new TransactionSourceInputTipList(type);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 获取输入提示热门话题列表
     * @return
     */
    public int doTopicInputTipList() {
        XoneBaseTransaction t = new TransactionTopicInputTipList();
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 获取输入提示订阅主题列表
     * @return
     */
    public int doSubjectInputTipList() {
        XoneBaseTransaction t = new TransactionSubjectInputTipList();
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 图片，视频，音频上传
     * @param prama
     * @return
     */
    public int doUpload(UploadPrama prama) {
        XoneBaseTransaction t = new TransactionUpload(prama);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 发表讨论
     * @param content
     * @param imageUrl
     * @param audioUrl
     * @return
     */
    public int doPostDiscuss(String content, String imageUrl, UploadPrama audio) {
        XoneBaseTransaction t = new TransactionPostDiscuss(content, imageUrl, audio);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 评论资讯
     * @param content
     * @param imageUrl
     * @param audio
     * @return
     */
    public int doCommentInfo(String content, String imageUrl, UploadPrama audio) {
        XoneBaseTransaction t = new TransactionCommentInfo(content, imageUrl, audio);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 转发资讯
     * @param content
     * @param imageUrl
     * @param audio
     * @return
     */
    public int doForwardInfo(String content, String imageUrl, UploadPrama audio) {
        XoneBaseTransaction t = new TransactionForwardInfo(content, imageUrl, audio);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 环回接口
     * @param obj
     * @return
     */
    public int doLoopBack(Object obj) {
        XoneBaseTransaction t = new TransactionLoopBack(obj);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * urs登录的用户当昵称有重复时的重新注册
     * @param username
     * @param pwd
     * @param nickname
     * @return
     */

    public int doUrsUserReg(String username,String pwd,String nickname){
        UrsUserRegTransaction t = new UrsUserRegTransaction(username, pwd,nickname);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 获得游戏、综合主题列表
     * @param type
     * @see XoneConsts.SUBJECT_LIST_TYPE
     * @return
     */
    public int doGetSubjectList(int type){
        GetSubjectListTransaction t = new GetSubjectListTransaction(type);
        beginTransaction(t);
        return t.getId();
    }

    /**
     *第三方用户注册
     * @param uid
     * @param accessToken
     * @param type
     * @param nickname
     * @param avatar
     * @return
     */

    public int doExternalUserReg(String uid,String accessToken,int type,String nickname,String avatar){
        ExternalUserRegTransaction t = new ExternalUserRegTransaction(uid,accessToken,type,nickname,avatar);
        beginTransaction(t);
        return t.getId();
    }
    /**
     *获取手机验证码
     * @param getType 注册/修改密码
     * @param phoneNumber
     * @return
     */

    public int doGetCellphoneCaptcha(int getType,String phoneNumber){
        GetCaptchaTrasaction t = new GetCaptchaTrasaction(getType,phoneNumber);
        beginTransaction(t);
        return t.getId();
    }

    public int doSearch(int type, String queryText){
        Transaction t;
        switch(type){
            case XoneConsts.SEARCH_TYPE.SUBJECT_AND_SOURCE:
                t = new SearchSubjectAndSourceTransaction(type, queryText);
                break;
            default:
                throw new IllegalArgumentException("search type illegal! see XoneConsts.SEARCH_TYPE");
        }
        beginTransaction(t);
        return t.getId();
    }
    /**
     *手机号码注册
     * @param phoneNumber
     * @param nickName
     * @param captcha
     * @param pw
     * @return
     */
    public int doCellPhoneReg(String phoneNumber,String nickName,String captcha,String pw){
        CellPhoneRegTransaction t = new CellPhoneRegTransaction(phoneNumber,nickName,captcha,pw);
        beginTransaction(t);
        return t.getId();
    }
    /**
     *手机号码注册
     * @param phoneNumber
     * @param captcha
     * @param pw
     * @return
     */
    public int doModifyCellPhonPw(String phoneNumber,String captcha,String pw){
        ModifyCellPhonePwTransaction t = new ModifyCellPhonePwTransaction(phoneNumber,captcha,pw);
        beginTransaction(t);
        return t.getId();
    }
    /**
     *注销帐号
     * @return
     */
    public int doLogout(){
        LogoutTransaction t = new LogoutTransaction();
        beginTransaction(t);
        return t.getId();
    }
    /**
     *匿名用户登录
     * @return
     */
    public int doAnomyousLogin(){
        return doLogin(ACCOUNT_TYPE.Anonymous, null, null);
    }

    /**
     * 获取最新资讯源
     * @return
     */
    public int doGetLatestSourceList(){
        GetLatestSourceListTransaction t = new GetLatestSourceListTransaction();
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 获取最热主题列表
     * @return
     */
    public int doGetHotestSubjectList(){
        GetHotestSubjectListTransaction t = new GetHotestSubjectListTransaction();
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 获取推荐主题列表
     * @return
     */
    public int doGetRecommendSubjectList(){
        GetRecommendSubjectListTransaction t = new GetRecommendSubjectListTransaction();
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 批量获取资讯ID
     * @param infoGroup
     * @param sortType
     * @param attach
     * @return
     */
    public int doGetInfoIds(int infoGroup, int sortType, int attach){
        XoneBaseTransaction t = new TransactionGetInfoIds(infoGroup, sortType, attach);
        beginTransaction(t);
        return t.getId();
    }
    /**
     * 同步绑定帐号
     * @param userKey
     * @return
     */
    public int doSyncBindingInfo(String userKey){
        XoneBaseTransaction t = new SyncBindingInfoTrasaction(userKey);
        beginTransaction(t);
        return t.getId();
    }

    /**
     *
     * @param subjectId 主题id
     * @param subjectName 主题名称
     * 优先检测subjectId，若subjectId为空，取subjectName
     * @return
     */
    public int doGetSubjectDetail(int subjectId, String subjectName){
        GetSubjectDetailTransaction t = new GetSubjectDetailTransaction(subjectId, subjectName);
        beginTransaction(t);
        return t.getId();
    }

    /**
     * 获取资讯列表
     * @param refresh
     * @param infoGroup
     * @param sortType
     * @param infoIds
     * @return
     */
    public int doGetInfoList(boolean refresh, int infoGroup, int sortType, String infoIds) {
        XoneBaseTransaction t = new TransactionGetInfoList(refresh, infoGroup, sortType, infoIds);
        beginTransaction(t);
        return t.getId();
    }
    /**
     * 上传反馈日志
     * @return
     */
    public int doPostLog(){
        Transaction t = new PostLogTransaction();
        beginTransaction(t);
        return t.getId();
    }
    /**
     * 上传反馈日志
     * @return
     */
    public int doFeedBack(int type,String user,String title,String content,String fileId,String contact,Context context){
        Transaction t = new FeedBackTransaction(type,user,title,content,fileId,contact,context);
        beginTransaction(t);
        return t.getId();
    }

    /**
    *
    * @param subjectId 主题id
    * @return
    */
   public int doGetSourceDetail(int sourceId){
       GetSourceDetailTransaction t = new GetSourceDetailTransaction(sourceId);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 订阅
    * @param subjectId 主题ID，与sourceId至少一个不为空，设空传-1
    * @param sourceId 资讯源ID，若为空，订阅整个主题下的所有资讯源，设空传-1
    * @return
    */
   public int doSubscribe(int subjectId, int sourceId){
       SubscribeTransaction t = new SubscribeTransaction(subjectId, sourceId);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 取消订阅
    * @param sourceId
    * @return
    */
   public int doUnsubscribe(int sourceId){
       UnsubscribeTransaction t = new UnsubscribeTransaction(sourceId);
       beginTransaction(t);
       return t.getId();
   }
   /**
    * 获取应用参数
    * @return
    */
   public int doGetAppSetting(){
       XoneBaseTransaction t = new GetAppSettingTransaction();
       beginTransaction(t);
       return t.getId();
   }
   /**
    * 设置应用参数
    * @return
    */
   public int doSetAppSetting(){
       XoneBaseTransaction t = new SetAppSettingTransaction();
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取资讯正文
    * @param infoId 资讯Id
    * @return
    */
   public int doGetInfoDetail(String infoId){
       XoneBaseTransaction t = new TransactionGetInfoDetail(infoId);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取资讯评论，转发，赞
    * @param infoId
    * @param type
    * @param param
    * @return
    */
   public int doGetInfoCfp(String infoId, int type, PageParam param) {
       XoneBaseTransaction t = new TransactionGetInfoCfp(infoId, type, param);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取用户订阅列表
    * @param userId
    * @return
    */
   public int doGetSubscribeList(int userId){
       XoneBaseTransaction t = new GetSubscribeListTransaction(userId);
       beginTransaction(t);
       return t.getId();
   }
   /**
    * 获取推荐App
    */
   public int doGetRecommApp(){
       GetRecommAppTransaction t = new GetRecommAppTransaction();
       beginTransaction(t);
       return t.getId();
   }
   /**
    * 检查应用版本号
    */
   public int doCheckAppVersion(){
       XoneBaseTransaction t = new CheckVersionTransaction();
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取未读资讯条数
    * @return
    */
   public int doGetUnReadInfoCount() {
       XoneBaseTransaction t = new TransactionUnReadInfoCount();
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 找朋友-人气榜
    * @return
    */
   public int doGetPopFriendList(int currentPage, int pageSize){
       XoneBaseTransaction t = new GetPopFriendListTransaction(currentPage, pageSize);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 找朋友-猜你喜欢
    * @return
    */
   public int doGetGuessFriendList(int currentPage, int pageSize){
       XoneBaseTransaction t = new GetGuessFriendListTransaction(currentPage, pageSize);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 发现讨论-最热
    * @return
    */
   public int doGetPopDiscussList(int currentPage, int pageSize){
       XoneBaseTransaction t = new GetPopDiscussListTransaction(currentPage, pageSize);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 发现讨论-猜你喜欢
    * @return
    */
   public int doGetGuessDiscussList(int currentPage, int pageSize){
       XoneBaseTransaction t = new GetGuessDiscussListTransaction(currentPage, pageSize);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 赞资讯
    * @param infoId
    * @param praiseFlag
    * @return
    */
   public int doPraiseInfo(String infoId, boolean praiseFlag) {
       XoneBaseTransaction t = new TransactionPraiseInfo(infoId, praiseFlag);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取热门话题
    * @return
    */
   public int doGetTopicList(int count){
       XoneBaseTransaction t = new GetTopicListTransaction(count);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取用户信息
    * @return
    */
   public int doGetUserDetail(String userId){
       XoneBaseTransaction t = new GetUserDetailTransaction(userId);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取用户的粉丝列表
    */
   public int doGetFansList(String userId, int count){
       XoneBaseTransaction t = new GetFansListTransaction(userId, count);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取用户的关注列表
    */
   public int doGetFollowList(String userId, int count){
       XoneBaseTransaction t = new GetFollowListTransaction(userId, count);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 关注
    * @param userId
    * @return
    */
   public int doFollow(String userId){
       XoneBaseTransaction t = new FollowTransaction(userId);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 取消关注
    * @param followedUserId
    * @return
    */
   public int doCancelFollow(String userId){
       XoneBaseTransaction t = new CancelFollowTransaction(userId);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取资讯源下资讯列表
    * @param sourceId
    * @return
    */
   public int doGetSourceInfoList(String sourceId, int type, int count, long maxTime){
       XoneBaseTransaction t = new TransactionGetSourceInfoList(sourceId ,type, count, maxTime);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取主题下讨论列表
    * @param sourceId
    * @return
    */
   public int doGetSubjectInfoList(int subjectId, int currentPage ,int pageSize){
       XoneBaseTransaction t = new TransactionGetSubjectInfoList(subjectId, currentPage ,pageSize);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取话题下讨论列表
    * @param sourceId
    * @return
    */
   public int doGetTopicInfoList(long topicId, int currentPage ,int pageSize){
       XoneBaseTransaction t = new TransactionGetTopicInfoList(topicId, currentPage, pageSize);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 修改用户信息
    * @return
    */
   public int doUpdateUser(User user){
       XoneBaseTransaction t = new UpdateUserTransaction(user);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获得用户的好友动态id列表
    * @return
    */
   public int doGetDynamicIds(int allCount, int returnCount){
       XoneBaseTransaction t = new GetDynamicIdsTransaction(allCount, returnCount);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获得用户的好友动态消息列表
    * @return
    */
   public int doGetDynamicList(String[] ids){
       XoneBaseTransaction t = new GetDynamicListTransaction(ids);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取通知列表
    * @return
    */
   public int doGetAdaviceList(){
       XoneBaseTransaction t = new GetAdaviceListTransaction();
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 反馈官方资讯
    * @param infoId
    * @param type
    * @param message
    * @return
    */
   public int doFeedBack(String infoId, int type, String message){
       XoneBaseTransaction t = new TransactionFeedback(infoId, type, message);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 举报用户讨论
    * @param infoId
    * @param type
    * @param message
    * @return
    */
   public int doReport(String infoId, int type, String message){
       XoneBaseTransaction t = new TransactionReport(infoId, type, message);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取被评论过的讨论列表
    * @param sourceId
    * @return
    */
   public int doGetCommentDiscussList(int count, long maxTime, long sinceTime){
       XoneBaseTransaction t = new TransactionGetCommentDiscussList(count ,maxTime, sinceTime);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取@我的讨论列表
    * @param sourceId
    * @return
    */
   public int doGetAtDiscussList(int type, int count, long maxTime, long sinceTime){
       XoneBaseTransaction t = new TransactionGetAtDiscussList(type, count, maxTime, sinceTime);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取赞我的讨论列表
    * @param sourceId
    * @return
    */
   public int doGetPraiseDiscussList(boolean refresh, int count, long maxTime, long sinceTime){
       XoneBaseTransaction t = new TransactionGetPraiseDiscussList(refresh, count, maxTime, sinceTime);
       beginTransaction(t);
       return t.getId();
   }

   /**
    * 获取赞我的讨论列表
    * @param sourceId
    * @return
    */
   public int doGetUserDiscussList(boolean refresh, String userId, int count, long maxTime, long sinceTime){
        XoneBaseTransaction t = new TransactionGetUserDiscussList(refresh, userId, count, maxTime, sinceTime);
       beginTransaction(t);
       return t.getId();
   }
}